<template>
  <div class="table-dom">
    <div class="table-box table-title">
      <div class="item1">Product</div>
      <div class="item2">
        <div class="item2-i">Price</div>
        <div class="item2-i">+/-</div>
        <div class="item2-i">Type</div>
      </div>
    </div>
    <van-swipe-cell
      v-for="(i,index) in list"
      :key="index"
      class="router-link-bg table-item needsclick"
      :class="'table-item-'+i.flag"
    >
      <div class="table-box" @click="getDtails('/priceDetail',i)">
        <div class="item1">
          <p>{{i.kname}}</p>
        </div>
        <div class="item2">
          <div class="item2-i table-item-price">{{i.price}}</div>
          <div class="item2-i table-item-status">
            <div class="status-but">{{i.gain}}</div>
          </div>
          <div class="item2-i">{{i.origin}}</div>
        </div>
      </div>
      <template slot="right">
        <div class="del" @click="updateyprice(i,index)"></div>
      </template>
    </van-swipe-cell>
    <nullDom v-if="nullDomShow" style="color:#697274;background: #ffffff;"></nullDom>
  </div>
</template>
<script type="text/ecmascript-6">
import { getmychannelpricelist, updateyprice } from "../../api/price";
import nullDom from "../../components/nulllist/nullList";
export default {
  data() {
    return {
      list: [],
      nullDomShow: false
    };
  },
  components: {
    nullDom
  },

  computed: {
    pricepartObj() {
      return this.$store.state.nocaches.pricepartObj;
    }
  },
  watch: {
    pricepartObj(news, old) {
      this.getmychannelpricelists();
    }
  },
  methods: {
    getDtails(url, query) {
      this.$router.push({ path: url, query: query });
    },
    getmychannelpricelists() {
      this.list = [];
      let obj = {};
      obj.channel = this.$store.state.nocaches.pricepartObj.channel;
      let loginInforXl = this.$store.state.loginInforXl;
      if (obj.channel == "tcl") {
        obj.myprice = loginInforXl.tcl_myprice;
      } else if (obj.channel == "dc") {
        obj.myprice = loginInforXl.dc_myprice;
      } else {
        obj.myprice = loginInforXl.dlg_myprice;
      }
      if (obj.myprice.length > 0) {
        obj.myprice = obj.myprice.join(",");
      } else {
        obj.myprice = "";
      }
      getmychannelpricelist(obj).then(res => {
        res.data = res.data || [];
        if (res.code == "0000") {
          if (res.data.length == 0) {
            this.nullDomShow = true;
            return;
          } else {
            this.nullDomShow = false;
            for (let index = 0; index < res.data.length; index++) {
              const element = res.data[index];
              if (element.select == 1) {
                element.select = true;
              } else {
                element.select = false;
              }
              if (element.flag == "normal") {
                element.gain = "--";
                element.per = "--";
              } else if (element.flag == "up") {
                element.gain = "+" + element.gain;
                element.per = "+" + element.per;
              }
            }
          }
          this.list = res.data;
        } else if (res.code == "0007" || res.code == "0009") {
          if (res.code == "0007" ) {
              this.$toast(res.msg)
              this.isLogin = true;
          }else{
              this.isLogin = false;
          }
          if (res.data.length == 0) {
            this.nullDomShow = true;
          } else {
            this.nullDomShow = false;
            for (let index = 0; index < res.data.length; index++) {
              const element = res.data[index];
              element.select = false;
            }
            this.list = res.data;
          }
        } else if (res.code == "1100") {
          this.nullDomShow = true;
        }
      });
    },
    updateyprice(i, index) {
      let obj = {};
      obj.channel = i.channel;
      obj.id = i.id;
      obj.op = "del";
      updateyprice(obj).then(res => {
        if (res.code == "0000") {
          this.$store.dispatch("acLoginprice", obj);
          this.$store.dispatch("acdelprice", i);
          this.list.splice(index, 1);
        } else {
          this.$toast(res.msg);
        }
      });
    }
  },
  created() {
    this.getmychannelpricelists();
  }
};
</script>
<style scoped lang="less">
.table-box {
  display: flex;
  padding: 0.25rem 0;
  .item1 {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .item2 {
    flex: 2;
    display: flex;

    align-items: center;
    .item2-i {
      flex: 1;
    }
  }
}
.table-dom {
  background: rgba(243, 246, 249, 1);
  position: relative;
  .table-title {
    height: 0.8rem;
    line-height: 0.8rem;
    background: #ffffff;
    font-size: 0.28rem;
    text-align: center;
    color: #697274;
    margin-bottom: 0.1rem;
  }
}
.table-item {
  text-align: center;
  color: rgba(39, 41, 55, 1);
  font-size: 0.26rem;
  background: #ffffff;
  border-bottom: 0.02rem solid rgba(243, 246, 249, 1);
     table-layout: fixed;
    table-layout: fixed;
    word-wrap: break-word;
    word-break: keep-all;
    word-wrap: break-word;
  .status-but {
    font-size: 0.24rem;
    background: rgba(243, 246, 249, 1);
    line-height: 0.4rem;
    position: relative;
    border-radius: 0.05rem;
    overflow: hidden;
    display: inline;
    padding: 0.05rem 0.2rem;
    color: rgba(105, 114, 116, 1);
  }
  &.table-item-down {
    .table-item-price {
      color: #37a10c;
    }
    .status-but {
      background: #00af49;
      color: #ffffff;
    }
  }
  &.table-item-up {
    .table-item-price {
      color: #d82d27;
    }
    .status-but {
      background: #d82d27;
      color: #ffffff;
    }
  }
}
.del {
  width: 0.66rem;
  height: 0.66rem;
  background: url("./optional-delete@2x.png") no-repeat;
  background-size: contain;
}
</style>
